//
//  ViewController.m
//  20-XML-SAXAndDom
//
//  Created by 王道林 on 16/8/10.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "ViewController.h"
#import "GDataXMLNode.h"
#import "Student.h"
@interface ViewController ()<NSXMLParserDelegate>
@property (nonatomic, strong)NSMutableArray *stuArray;
@property (nonatomic, copy) NSString *currentElementName;
@property (nonatomic, strong) NSMutableArray *DomArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

// xml  分为俩种解析方式
// 第一种: SAX 逐行解析 --- 使用系统的解析器 NSXMLParser
- (IBAction)saxParse:(id)sender {
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *xmlData = [NSData dataWithContentsOfFile:path];
    
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:xmlData];
    xmlParser.delegate = self;
    [xmlParser parse];
    
}

// 1. 解析器开始
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    self.stuArray = [NSMutableArray array];
}
// 2. 遇到节点头  调用的方法
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict
{
  //  NSLog(@"节点的头%@",elementName);
   //  记录节点头
    _currentElementName = elementName;
    if ([elementName isEqualToString:@"student"]) {
        Student *stu = [Student new];
        [_stuArray addObject:stu];
    }
    
}

//3. 遇到节点值. 调用的方法. 没有值时 对应的结果\n\t...
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    // string 是 \n\t
    if (![string hasSuffix:@" "] || ![string hasSuffix:@"\n"]) {
        Student *stu = [_stuArray lastObject];
        [stu setValue:string forKey:_currentElementName];
    }
    NSLog(@"节点的值%@", string);
}

// 4. 遇到节点尾, 调用的方法
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    _currentElementName = nil;
    NSLog(@"节点尾/%@", elementName);
}

// 5.解析器结束
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    NSLog(@"%@", _stuArray);
}

// 6. 解析失败时,调用
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"解析失败:%@", parseError);
}







// 第二种解析..DOM解析
//  关于GDataXMLNode 的引入
// 1 . 引入.h与.m 文件
// 2. 引入系统动态库libxml2.tbd
// 3. 根据GDataXMLNode头文件的提示, 添加/user/include/libxml2
// 4. 如果是ARC环境, 需要该文件进行MRC编译, 添加-fno-objc-arc
- (IBAction)domParse:(id)sender {
    
    _DomArray = [NSMutableArray array];
    NSString *pathData = [[NSBundle mainBundle] pathForResource:@"stu_xml" ofType:@"xml"];
    NSData *data = [NSData dataWithContentsOfFile:pathData];
    // 1. 创建GDataXMLDocument对象
    // 2. 根据XML的Data数据生成一个树形结构的Document
    NSError *error = nil;
    GDataXMLDocument *decument = [[GDataXMLDocument alloc] initWithData:data options:0 error:&error];
    // 3. 根据Document 获取根节点
    GDataXMLElement *rootElement = [decument rootElement];
    
    // 4. 根据根节点获取, 子节点数据
    NSArray *rootChildElement = rootElement.children;
    
    for (GDataXMLElement *element in rootChildElement) {
        
        // 5. 对应所有student节点
        Student *stu = [Student new];
        [self.DomArray addObject:stu];
   //     NSLog(@"childElement name :%@", element.name);
        // 6.遍历student节点的所有子节点
        for (GDataXMLElement *grandSonElement in element.children ) {
            // 7.赋值
            [stu setValue:grandSonElement.stringValue forKey:grandSonElement.name];
            
            
   //         NSLog(@"grandSon:%@", grandSonElement);
        }
    }
    NSLog(@"%@", _DomArray);
    
}




// 第三种    XMLReader
// 将XML结构, 转化成Dictionary








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
